package com.gjt.thread;

import com.gjt.thread.Thread.*;
import com.gjt.thread.entity.InstanceCallDigest;

//import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.*;

/**
 * Created with Intel IDEA.
 * 执行线程的Main
 *
 *
 * @Auther: gjt
 * @Date: 2024/3/20 16:21
 */
public class Main {


    /**
     * case 3的
     */
    public static void receiveDigest(byte[] digest, String name){
        StringBuilder result = new StringBuilder(name);
        result.append(": ");
//        result.append(DatatypeConverter.printHexBinary(digest));
        System.out.println(result);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {

        /**
         * case1 : Runnable
          */
//        ExecutorService executorService = Executors.newFixedThreadPool(5, new ThreadPoolFactory("calDigest"));
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        // execute 只能执行Runnable 而submit可以执行Runnable和Callable
        executorService.execute(new DigestRunnable(args[0]));


        /**
         * case2 : Thead 有很大问题 一般不用
         */
        ReturnDigestThread reDigest = new ReturnDigestThread(args[0]);
        reDigest.start();

        //获取线程执行的返回结果
        byte[] digest = reDigest.getDigest();
        while(true){
            if(digest != null){
//               System.out.println(DatatypeConverter.printHexBinary(digest));
               break;
            }
        }

        /**
         * case 3: 回调线程的返回值 使用静态方法
         */
        CallBackDigestRunnable callBackDigest = new CallBackDigestRunnable(args[0]);
        Thread t = new Thread(callBackDigest);
        t.start();

        /**
         * case 4 :回调线程的返回值 使用实列的方法  观察者模式
         */
        InstanceCallDigest ist = new InstanceCallDigest(args[0]);
        ist.calculateDigest();

        /**
         *  case 5: 使用线程简单选择排序
         */

        int[] data = {7,78,33,53,9,199,33,44};
        // 简单排序程序
        SortedCallable sortedCallable = new SortedCallable(data);

        ExecutorService service = Executors.newFixedThreadPool(2);
        Future<int[]> future = service.submit(sortedCallable);

        future.get();

        for (int datum : data) {
            System.out.print(datum+" ");
        }

    }


    public void print() throws IOException, ExecutionException, InterruptedException {
        char[] list = new char[100];
        /**
         * case 6；这个线程读取一个文件的读取全部;另一个线程读前n个字节
         */
        InputStream in = Files.newInputStream(Paths.get("D:\\Idea\\项目\\myboot\\socket\\src\\main\\resources\\hello.txt"));
        ReadPartFileThread readPartFileThread = new ReadPartFileThread(this,in);
        synchronized (in){
            FutureTask<char[]> futureTask = new FutureTask(readPartFileThread);
            new Thread(futureTask).start();
            try{
                in.wait();
                char[] ints = futureTask.get();
                for (int i = 0; i < ints.length; i++) {
                    list[i] = ints[i];
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }


        }
    }
}
